#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //初始化场景
    initScene();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initScene()
{
    //随机数种子
    srand((unsigned int)time(NULL));
    //初始化窗口大小
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //设置窗口标题
    setWindowTitle(GAME_TITLE);
    //设置图标资源
    setWindowIcon(QIcon(GAME_ICON));
    //定时器设置
    m_Timer.setInterval(GAME_RATE);
    playGame();
    m_recorder = 0;
    //得分记录
    score=new QLCDNumber;
    score->setParent(this);
    //设置字体
    score->setDecMode();
    //除去边框
    score->setFrameStyle(0);
    //位置
    score->move(10,10);
    //大小
    score->setFixedSize(QSize(200,50));
    //文字
    QLabel*lab=new QLabel(this);
    QPixmap pix;
    pix.load(":/app.png");
    lab->setPixmap(pix);
    lab->show();
    lab->move(15,20);
    connect(this,&MainWindow::explosion,[=](){
        this->num+=10;
        //显示分数
        score->display(num);
    });
}
void MainWindow::playGame()
{
    //启动定时器
    m_Timer.start();
    //监听定时器
    connect(&m_Timer,&QTimer::timeout,[=]()
    {
        //敌机出场
        enemyToScene();
        //更新游戏中元素的坐标
        updatePosition();
        //重新绘制图片
        update();
        //碰撞检测
        collisionDetection();

    });
    //启动背景音乐
    QSound::play(SOUND_BACKGROUND);
}
void MainWindow::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();
    //测试子弹代码
    //temp_bullet.m_Free = false;
    //temp_bullet.updatePosition();
    //发射子弹
    m_hero.shoot();
    //计算子弹坐标
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
        //如果子弹状态为非空闲，计算发射位置
        if(!m_hero.m_bullets[i].m_Free)
        {
            m_hero.m_bullets[i].updatePosition();
        }
    }
    //敌机坐标计算
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        //非空闲敌机 更新坐标
        if(m_enemys[i].m_Free == false)
        {
            //发射子弹
            m_enemys[i].shoot();
            m_enemys[i].updatePosition();
            //敌机子弹更新坐标
            for(int j=0;j<ENEMY_BULLET_NUM;j++)
            {
                //如果非空闲，计算发射位置
                if(m_enemys[i].enemy_bullets[j].m_Free==0)
                {
                    m_enemys[i].enemy_bullets[j].update_Enemy_Bullet_Position();
                }
            }
        }
    }
    //计算爆炸播放的图片
    for(int i = 0 ; i < BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free == false)
        {
            m_bombs[i].updateInfo();
        }
    }

}
void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY , m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY , m_map.m_map2);
    //绘制英雄
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);
    // 绘制战机血条
    painter.drawPixmap(400,0,pix_blood);
    //填充血条颜色
    QBrush bru(QColor(0,255,0));
    //放置血条
    painter.setBrush(bru);
    //计算血条比例
    float rate=1.0*m_hero.m_Hero_Blood/m_blood_num;
    if(rate<=0)
    {
        rate=0;
    }
    //绘制血条
    painter.drawRect(475,33,rate*(pix_blood.width()-85),24);
    //测试子弹代码
    //painter.drawPixmap(temp_bullet.m_X,temp_bullet.m_Y,temp_bullet.m_Bullet);
    //绘制子弹
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
        //如果子弹状态为非空闲，计算发射位置
        if(!m_hero.m_bullets[i].m_Free)
        {
            painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
        }
    }
    //绘制敌机
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free == false)
        {
            painter.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
            //填充血条颜色
            QBrush bru(QColor(255,0,0));
            //放置血条
            painter.setBrush(bru);
            //计算血条比例
            float rate=1.0*m_enemys[i].m_Blood/ENEMY_BLOOD;
            //绘制血条
            painter.drawRect(m_enemys[i].m_X,m_enemys[i].m_Y,rate*m_enemys[i].m_Rect.width(),10);
            //绘制敌机子弹
            for(int j=0;j<ENEMY_BULLET_NUM;j++)
            {
                //如果非空闲，绘制
                if(m_enemys[i].enemy_bullets[j].m_Free==0)
                {
                    painter.drawPixmap(m_enemys[i].enemy_bullets[j].m_x,m_enemys[i].enemy_bullets[j].m_y, m_enemys[i].enemy_bullets[j].m_Enemy_Bullet);
                }
            }
        }
    }
    //绘制爆炸图片
    for(int i = 0 ; i < BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free == false)
        {
            painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
        }
    }

}
void MainWindow::keyPressEvent(QKeyEvent *e)
{
    if(e->key() == Qt::Key_Q)
    {
        qApp->quit();//关闭程序
    }
    if(e->key()==Qt::Key_Space)
    {
        if(!isStart)
        {
            tid = this->startTimer(GAME_RATE);//加速
            isStart = true;
        }
        else
        {
            this->killTimer(tid);
            isStart = false;
        }
        return;
    }
    switch(e->key())
    {
    case Qt::Key_Up:
        up();
        break;
    case Qt::Key_Down:
        down();
        break;
     case Qt::Key_Left:
        left();
        break;
    case Qt::Key_Right:
        right();
        break;
    }
}
void MainWindow::up()
{
    int x = m_hero.m_X;
    int y = m_hero.m_Y - HERO_MOVE_STEP;//飞机上升
    if(y <= 0)//边界右移
        {
        y = 0;
        }
    if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
        {
        y = GAME_HEIGHT - m_hero.m_Rect.height();
        }
    m_hero.setPosition(x,y);
}
void MainWindow::down()
{
    int x = m_hero.m_X;
    int y = m_hero.m_Y + HERO_MOVE_STEP;//飞机下降
    if(y <= 0)//边界检测
        {
        y = 0;
        }
    if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
        {
        y = GAME_HEIGHT - m_hero.m_Rect.height();
        }
    m_hero.setPosition(x,y);
}
void MainWindow::left()
{
    int x = m_hero.m_X - HERO_MOVE_STEP;//飞机左移
    int y = m_hero.m_Y;
    if(x <= 0 )//边界检测
        {
        x = 0;
        }
    if(x >= GAME_WIDTH - m_hero.m_Rect.width())
        {
        x = GAME_WIDTH - m_hero.m_Rect.width();
        }
    m_hero.setPosition(x,y);
}
void MainWindow::right()
{
    int x = m_hero.m_X + HERO_MOVE_STEP;//飞机右移
    int y = m_hero.m_Y;
    if(x <= 0 )//边界检测
        {
        x = 0;
        }
    if(x >= GAME_WIDTH - m_hero.m_Rect.width())
        {
        x = GAME_WIDTH - m_hero.m_Rect.width();
        }
    m_hero.setPosition(x,y);
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    int x = event->x() - m_hero.m_Rect.width()*0.5; //鼠标位置 - 飞机矩形的一半
    int y = event->y() - m_hero.m_Rect.height()*0.5;
    //边界检测
    if(x <= 0 )
    {
        x = 0;
    }
    if(x >= GAME_WIDTH - m_hero.m_Rect.width())
    {
        x = GAME_WIDTH - m_hero.m_Rect.width();
    }
    if(y <= 0)
    {
        y = 0;
    }
    if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
    {
        y = GAME_HEIGHT - m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);
}
void MainWindow::enemyToScene()
{
    m_recorder++;
    if(m_recorder < ENEMY_INTERVAL)
    {
        return;
    }
    m_recorder = 0;
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            //敌机空闲状态改为false
            m_enemys[i].m_Free = false;
            //设置坐标
            m_enemys[i].m_X = rand() % (GAME_WIDTH - m_enemys[i].m_Rect.width());
            m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
            break;
        }
    }
}
void MainWindow::collisionDetection()
{
    //遍历所有非空闲的敌机
    for(int i = 0 ;i < ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            //空闲飞机 跳转下一次循环
            continue;
        }
        //敌机与战机的碰撞：如果和战机相交，
        if(m_enemys[i].m_Rect.intersects(m_hero.m_Rect))
        {
            //m_timer.stop();
            qDebug()<<m_hero.m_Hero_Blood<<endl;
            m_hero.m_Hero_Blood--;
            m_enemys[i].m_Blood--;
        }
        //遍历所有 非空闲的子弹
        for(int j = 0 ; j < BULLET_NUM;j++)
        {
            if(m_hero.m_bullets[j].m_Free)
            {
                //空闲子弹 跳转下一次循环
                continue;
            }
            //如果子弹矩形框和敌机矩形框相交，发生碰撞
            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
            {
                m_enemys[i].m_Blood -= m_hero.m_ATK;
                m_hero.m_bullets[j].m_Free = 1;
            }
        }
        //如果敌机血量小于0，爆炸
        if(m_enemys[i].m_Blood<=0)
        {
            //重置敌机状态
            m_enemys[i].m_Free=1;
            m_enemys[i].m_Blood=ENEMY_BLOOD;
            //敌机子弹消失
            for(int k=0;k<ENEMY_BULLET_NUM;k++)
            {
                m_enemys[i].enemy_bullets[k].m_Free=1;
            }
            //播放爆炸效果
            for(int k=0;k<BOMB_NUM;k++)
            {
                if(m_bombs[k].m_Free)
                {
                    //空闲的爆炸 播放
                    m_bombs[k].m_Free=0;
                    //更新爆炸坐标
                    m_bombs[k].m_X=m_enemys[i].m_X;
                    m_bombs[k].m_Y=m_enemys[i].m_Y;
                    //播放音效
                    QSound::play(SOUND_BOMB);
                    break;
                }
            }
            emit(explosion());
        }
        //游戏结束
        if(m_hero.m_Hero_Blood<=0)
        {
            m_Timer.stop();
            //将结束图片移动下来
            QPropertyAnimation*animation=new QPropertyAnimation(overlable,"geometry");
            //设置时间间隔
            animation->setDuration(1000);
            //设置开始位置
            animation->setStartValue(QRect(overlable->x(),overlable->y(),overlable->width(),overlable->height()));
            //结束位置
            animation->setEndValue(QRect(overlable->x(),overlable->y()+550,overlable->width(),overlable->height()));
            //曲线
            animation->setEasingCurve(QEasingCurve::OutBounce);
            //执行
            animation->start();
        }
    }
}
